package com.voicechat.firstmet.net.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.NetInterface;
import com.voicechat.firstmet.net.Params;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**网络请求Retrofit 代理对象
 * Created by Administrator on 2018/8/27.
 */

public class BaseService {
    public static NetInterface sNetInterface = retrofit().create(NetInterface.class);
   // public static NetInterface sNetInterfaceBack = retrofitBack().create(NetInterface.class);
    public static Gson gson = new GsonBuilder().serializeNulls().create();

    public static Retrofit retrofit() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.readTimeout(15, TimeUnit.SECONDS);
        builder.connectTimeout(15, TimeUnit.SECONDS);
        OkHttpClient client = builder.build();
        return new Retrofit.Builder().baseUrl(HTTPAPI.BASE_URL)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * Schedulers.io():
     * I/O 操作（读写文件、读写数据库、网络信息交互等）所使用的 Scheduler。
     *AndroidSchedulers.mainThread():
     * 主线程
     *
     * observeOn() 指定 Subscriber 线程
     subscribeOn 制定 Observable 线程

     */
    protected static <T> Observable.Transformer<T, T> applySchedulers() {
        return stringObservable ->
                stringObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    protected static <T> Observable.Transformer<String, T> json2model(final Class<T> clazz) {
        return observable ->
                observable.map((Func1<String, T>) s ->
                        gson.fromJson(s, clazz));
    }


    protected static Observable<String> fetch(Params params) {
        return retrofit().create(NetInterface.class).get(params).compose(applySchedulers());
    }

    protected static <T> Observable<T> fetch(Params params, Class<T> clazz) {
        return fetch(params).compose(json2model(clazz));
    }

    /*protected static Observable<String> upload(Params params, Map<String, RequestBody> partMap) {
        return retrofit().create(NetInterface.class).upload(params, partMap).compose(applySchedulers());
    }

    protected static <T> Observable<T> upload(Params params, Map<String, RequestBody> partMap, Class<T> clazz) {
        return upload(params, partMap).compose(json2model(clazz));
    }

    public static Observable<ResponseBody> download(String fileUrl) {
        return retrofit().create(NetInterface.class).download(fileUrl).compose(applySchedulers());
    }*/

}
